﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using VisualPureBasicIDE.CoreGUI.Properties;
using WeifenLuo.WinFormsUI.Docking;

namespace VisualPureBasicIDE.CoreGUI
{
    public partial class BuildOutputForm : DockContent
    {
        readonly ComponentResourceManager _resources = new ComponentResourceManager(typeof(BuildOutputForm));

        public BuildOutputForm()
        {
            InitializeComponent();
        }

        private void toolStripButtonCommands_Click(object sender, EventArgs e)
        {
            toolStripProgressBarBuildStatus.Visible = true;
            toolStripProgressBarBuildStatus.Value = 0;
            backgroundWorkerCommands.RunWorkerAsync();
        }

        private void backgroundWorkerCommands_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (Settings.Default.CompilerExecutablePath.Length == 0) return;

                var prcs = new Process
                               {
                                   StartInfo =
                                       {
                                           FileName = Settings.Default.CompilerExecutablePath,
                                           Arguments = "/?",
                                           RedirectStandardOutput = true,
                                           RedirectStandardError = true,
                                           UseShellExecute = false,
                                           CreateNoWindow = true
                                       }
                               };
                prcs.Start();

                var stdout = prcs.StandardOutput;
                e.Result = stdout.ReadToEnd();
            }
            catch (Exception ex) { Logger.SendException(ex); }
        }

        private void backgroundWorkerCommands_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            textBoxOutput.Text = (string)e.Result;
            toolStripProgressBarBuildStatus.Value = 100;
            timerHideProgressBar.Start();
            textBoxOutput.ScrollToCaret();
        }

        private void timerHideProgressBar_Tick(object sender, EventArgs e)
        {
            toolStripProgressBarBuildStatus.Visible = false;
            timerHideProgressBar.Stop();
        }

        private void toolStripButtonClearAll_Click(object sender, EventArgs e)
        {
            textBoxOutput.Text = String.Empty;
        }

        private void toolStripButtonWordWrap_Click(object sender, EventArgs e)
        {
            if (textBoxOutput.WordWrap) { textBoxOutput.WordWrap = false; toolStripButtonWordWrap.ToolTipText = _resources.GetString("strActivateWordWrap"); } else { textBoxOutput.WordWrap = true; toolStripButtonWordWrap.ToolTipText = _resources.GetString("strDeactivateWordWrap"); }
        }

        private void fromPBSourceFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fromPBSourceFileToolStripMenuItem.Checked = true;
            fromProjectToolStripMenuItem.Checked = false;
            fromSolutionToolStripMenuItem.Checked = false;
            // Stub
        }

        private void fromProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fromPBSourceFileToolStripMenuItem.Checked = false;
            fromProjectToolStripMenuItem.Checked = true;
            fromSolutionToolStripMenuItem.Checked = false;
            // Stub
        }

        private void fromSolutionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fromPBSourceFileToolStripMenuItem.Checked = false;
            fromProjectToolStripMenuItem.Checked = false;
            fromSolutionToolStripMenuItem.Checked = true;
            // Stub
        }
    }
}